﻿using DevComponents.DotNetBar;
using QLDVu.BO;
using QLKNCSDL.KetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class ThemPhieuBaoGiaForm : Office2007Form
    {
        int index = 0;
        public static string machitiet;
        public static string madichvu;
        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        public ThemPhieuBaoGiaForm()
        {
            InitializeComponent();
        }

        private void ThemPhieuBaoGiaForm_Load(object sender, EventArgs e)
        {
            var dsxe = from xe in BO.DanhSachXeBO.DanhSachXe()
                       select new { xe.BienSoXe, xe.HANGSX.TenHangSanXuat, xe.DONGXE.TenDongXe, xe.DOIXE.TenDoiXe };
            cbBienSoXe.DataSource = dsxe;
            cbBienSoXe.DisplayMember = "BienSoXe";
            cbBienSoXe.ValueMember = "BienSoXe";
            cbBienSoXe.DropDownColumns = "BienSoXe,TenHangSanXuat,TenDongXe,TenDoiXe";
            cbBienSoXe.DropDownColumnsHeaders = "Biển Số\r\nHãng SX\r\nDòng Xe\r\nĐời Xe";
            cbBienSoXe.DropDownWidth = 450;
            var dskhachhang = from kh in BO.DanhSachKhachHangBO.DanhSachKhachHang()
                              select new { kh.MaKhachHang, kh.TenKhachHang, kh.LOAIKHACHHANG.TenLoaiKhachHang, kh.DienThoai, kh.DiaChi, kh.Email, kh.KHUVUC.TenKhuVuc };

            cbKhachHang.DataSource = dskhachhang;
            cbKhachHang.DisplayMember = "TenKhachHang";
            cbKhachHang.ValueMember = "MaKhachHang";
            cbKhachHang.DropDownColumns = "TenKhachHang,TenLoaiKhachHang,DienThoai";
            cbKhachHang.DropDownColumnsHeaders = "Tên Khách Hàng\r\nLoại Khách hàng\r\nĐiện Thoại";
            cbKhachHang.DropDownWidth = 450;

            cbNhanVien.DataSource = BO.DanhSachNhanVienBO.DanhSachNhanVien();
            cbNhanVien.DisplayMember = "TenNguoiDung";
            cbNhanVien.ValueMember = "MaNguoiDung";
            cbNhanVien.DropDownColumns = "MaNguoiDung,TenNguoiDung";
            cbNhanVien.DropDownColumnsHeaders = "Mã Nhân Viên\r\nTên Nhân Viên\r\n";
            cbNhanVien.DropDownWidth = 200;

            dpNgayLapBaoGia.CustomFormat = "dd/MM/yyyy";
            dpNgayLapBaoGia.Format = DateTimePickerFormat.Custom;
            txtTongTien.Text = "0";
        }

        private void gvChiTiet_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {


                DataGridViewRow row = gvChiTiet.Rows[e.RowIndex];
                if (!row.IsNewRow)
                    gvChiTiet.Rows.Remove(row);
            }
            int tongtien = 0;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                {
                    tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                }
            }
            txtTongTien.Text = tongtien.ToString();
        }

        private void gvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            index = gvChiTiet.CurrentCell.RowIndex;
            ComboBox cb = e.Control as ComboBox;
            if (cb != null)
            {
                // first remove event handler to keep from attaching multiple:
                cb.SelectedIndexChanged -= new
                EventHandler(LoaiBaoGia_SelectedIndexChanged);

                // now attach the event handler
                cb.SelectedIndexChanged += new
                EventHandler(LoaiBaoGia_SelectedIndexChanged);
            }

            if (gvChiTiet.CurrentCell.ColumnIndex == 3)
            {
                TextBox tbSoLuong = (TextBox)e.Control;
                tbSoLuong.TextChanged += new EventHandler(tbSoLuong_TextChanged);
                tbSoLuong.KeyPress += tbSoLuong_KeyPress;

            }
            if (gvChiTiet.CurrentCell.ColumnIndex == 4)
            {
                TextBox tbDonGia = (TextBox)e.Control;
                tbDonGia.TextChanged += tbDonGia_TextChanged;
                tbDonGia.KeyPress += tbDonGia_KeyPress;
            }
          
        }
        void tbDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        void tbSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void LoaiBaoGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmbprocess = (ComboBox)sender;
            if (cmbprocess.Text != "" && cmbprocess.Text != null)
            {
                if (cmbprocess.Text.Equals("Dịch Vụ"))
                {
                    DanhSachDichVu frm = new DanhSachDichVu();
                    frm.Button_Clicked += new EventHandler(frm_btnDichVu_Clicked);
                    frm.Show();
                    cmbprocess.SelectedIndex = 1;
                }
                else
                {


                    DanhSachHangHoa frm = new DanhSachHangHoa();
                    frm.Button_Clicked += new EventHandler(frm_btnHangHoa_Clicked);
                    frm.Show();
                    cmbprocess.SelectedIndex = 0;
                }
                /// Your Code goes here
            }
        }

        private void tbDonGia_TextChanged(object sender, EventArgs e)
        {
            index = gvChiTiet.CurrentCell.RowIndex;
            int indecolum = gvChiTiet.CurrentCell.ColumnIndex;
            if (indecolum == 3)
            {
                TextBox soluong = (TextBox)sender;
                if (soluong.Text != "" && gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(soluong.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim());
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }


            }
            else if (indecolum == 4)
            {

                TextBox dongia = (TextBox)sender;
                if (dongia.Text != "" && gvChiTiet.Rows[index].Cells["SoLuong"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(dongia.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["SoLuong"].Value);
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }

            }
            int tongtien = 0;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                {
                    tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                }
            }
            txtTongTien.Text = tongtien.ToString();
        }

        private void tbSoLuong_TextChanged(object sender, EventArgs e)
        {
            index = gvChiTiet.CurrentCell.RowIndex;
            int indecolum = gvChiTiet.CurrentCell.ColumnIndex;
            if (indecolum == 3)
            {
                TextBox soluong = (TextBox)sender;
                if (soluong.Text != "" && gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(soluong.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["DonGia"].Value.ToString().Trim());
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }


            }
            else if (indecolum == 4)
            {

                TextBox dongia = (TextBox)sender;
                if (dongia.Text != "" && gvChiTiet.Rows[index].Cells["SoLuong"].Value.ToString().Trim() != "")
                {
                    int thanhtien = Convert.ToInt32(dongia.Text) * Convert.ToInt32(gvChiTiet.Rows[index].Cells["SoLuong"].Value);
                    gvChiTiet.Rows[index].Cells["ThanhTien"].Value = thanhtien.ToString();
                }

            }
            int tongtien = 0;
            for (int i = 0; i < gvChiTiet.RowCount; i++)
            {
                if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                {
                    tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                }
            }
            txtTongTien.Text = tongtien.ToString();

        }
        private void frm_btnHangHoa_Clicked(object sender, EventArgs e)
        {
            this.DuLieuHangHoa(machitiet);
        }
        void DuLieuHangHoa(string mahanghoa)
        {
            if (mahanghoa != "")
            {

                HANGHOA hanghoa = BO.DanhSachHangHoaBO.DanhSachHangHoa(mahanghoa);
                //bool kt = false;
                //int tongtien = 0;
                //if (kt == false)
                //{
                //    //string tenhanghoa = cbHangHoa.Text.Trim();
                //    //string mahanghoa = mahanghoa;

                //    dschitiet.Add(new ChiTietPhieuBaoGia(mahanghoa, hanghoa.TenHangHoa,
                //    Convert.ToInt32(txtSoLuong.Text.Trim()), Convert.ToInt32(txtDonGia.Text.Trim()),
                //    Convert.ToInt32(txtThanhTien.Text.Trim())));
                //}
                //foreach (ChiTietPhieuBaoGia chitiet in dschitiet)
                //{

                //    tongtien = tongtien + chitiet.ThanhTien;
                //}

                //txtTongTien.Text = tongtien.ToString();
                string dongia = Convert.ToInt32(ChiTietPhieuBaoGiaBO.DonGiaHangHoa(mahanghoa)).ToString();
                //txtDonGia.Text = dongia;
                DataGridViewRow row = gvChiTiet.Rows[index];

                row.Cells["ChiTietBaoGia"].Value = hanghoa.TenHangHoa;
                gvChiTiet.Rows[index].Cells["DonGia"].Value = dongia;
                gvChiTiet.Rows[index].Cells["SoLuong"].Value = "1";
                gvChiTiet.Rows[index].Cells["ThanhTien"].Value = dongia;
                gvChiTiet.Rows[index].Cells["LoaiBaoGia"].Value = "Hàng Hóa";
                gvChiTiet.Rows[index].Cells["MaChiTietBaoGia"].Value = hanghoa.MaHangHoa;
                gvChiTiet.Rows[index].Cells["Xoa"].Value = "Xóa Chi Tiết";
                int tongtien = 0;
                for (int i = 0; i < gvChiTiet.RowCount; i++)
                {
                    if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                    {
                        tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                    }
                }
                txtTongTien.Text = tongtien.ToString();
                gvChiTiet.CurrentCell = gvChiTiet.Rows[index].Cells["SoLuong"];
                gvChiTiet.BeginEdit(true);
                //gvChiTiet.Rows.Add(row);


            }
        }
        private void frm_btnDichVu_Clicked(object sender, EventArgs e)
        {
            this.DuLieuDichVu(madichvu);
        }
        void DuLieuDichVu(string madichvu)
        {
            if (madichvu != "")
            {

                DICHVU dichvu = BO.DanhSachDichVuBO.DanhSachDichVu(madichvu);
                //bool kt = false;
                //int tongtien = 0;
                //if (kt == false)
                //{
                //    //string tenhanghoa = cbHangHoa.Text.Trim();
                //    //string mahanghoa = mahanghoa;

                //    dschitiet.Add(new ChiTietPhieuBaoGia(mahanghoa, hanghoa.TenHangHoa,
                //    Convert.ToInt32(txtSoLuong.Text.Trim()), Convert.ToInt32(txtDonGia.Text.Trim()),
                //    Convert.ToInt32(txtThanhTien.Text.Trim())));
                //}
                //foreach (ChiTietPhieuBaoGia chitiet in dschitiet)
                //{

                //    tongtien = tongtien + chitiet.ThanhTien;
                //}

                //txtTongTien.Text = tongtien.ToString();
                string dongia = Convert.ToInt32(ChiTietPhieuBaoGiaBO.DanhSachDichVu(madichvu).TienCong).ToString();
                //txtDonGia.Text = dongia;
                DataGridViewRow row = gvChiTiet.Rows[index];

                row.Cells["ChiTietBaoGia"].Value = dichvu.TenDichVu;
                gvChiTiet.Rows[index].Cells["DonGia"].Value = dongia;
                gvChiTiet.Rows[index].Cells["SoLuong"].Value = "1";
                gvChiTiet.Rows[index].Cells["SoLuong"].ReadOnly = true;
                gvChiTiet.Rows[index].Cells["ThanhTien"].Value = dongia;
                gvChiTiet.Rows[index].Cells["LoaiBaoGia"].Value = "Dịch Vụ";
                gvChiTiet.Rows[index].Cells["MaChiTietBaoGia"].Value = dichvu.MaDichVu;
                gvChiTiet.Rows[index].Cells["Xoa"].Value = "Xóa Chi Tiết";
                int tongtien = 0;
                for (int i = 0; i < gvChiTiet.RowCount; i++)
                {
                    if (gvChiTiet.Rows[i].Cells["ThanhTien"].Value != null)
                    {
                        tongtien = tongtien + Convert.ToInt32(gvChiTiet.Rows[i].Cells["ThanhTien"].Value);
                    }
                }
                txtTongTien.Text = tongtien.ToString();
                gvChiTiet.CurrentCell = gvChiTiet.Rows[index].Cells["DonGia"];
                gvChiTiet.BeginEdit(true);
                //gvChiTiet.Rows.Add(row);


            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bool kiemtra = false;
            if (Convert.ToInt32(txtTongTien.Text) == 0)
            {
                MessageBox.Show("Bạn phải nhập chi tiết hàng hóa");
                kiemtra = true;
            }

            if (kiemtra == false)
            {
                string mabaogia2 = "";
                if (ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung() != null)
                    mabaogia2 = NextID(ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung().MaBaoGia, "BG");
                else mabaogia2 = "BG0001";
                ChiTietPhieuBaoGiaBO.ThemPhieuBaoGia(mabaogia2, cbKhachHang.SelectedValue.ToString(),
                    cbBienSoXe.SelectedValue.ToString(), cbNhanVien.SelectedValue.ToString(), dpNgayLapBaoGia.Value.ToShortDateString(),
                    txtTongTien.Text.Trim());
                string mabaogia = ChiTietPhieuBaoGiaBO.MaBaoGiaCuoiCung().MaBaoGia;
                for (int i = 0; i < gvChiTiet.RowCount; i++)
                {
                    string machitietbaogia;
                    if (ChiTietPhieuBaoGiaBO.MaChiTietBaoGiaCuoiCung() != null)
                        machitietbaogia = NextID(ChiTietPhieuBaoGiaBO.MaChiTietBaoGiaCuoiCung().MaChiTietBaoGia, "CTBG");
                    else
                        machitietbaogia = "CTBG0001";
                    string madichvu = null;
                    string mahanghoa = null;
                    if (gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value != null)
                    {
                        if (gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim().Substring(0, 2).Equals("DV"))
                        {
                            madichvu = gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim();
                        }
                        else
                        {
                            mahanghoa = gvChiTiet.Rows[i].Cells["MaChiTietBaoGia"].Value.ToString().Trim();
                        }


                        string dongia = gvChiTiet.Rows[i].Cells["DonGia"].Value.ToString().Trim();
                        string soluong = gvChiTiet.Rows[i].Cells["SoLuong"].Value.ToString().Trim();
                        string thanhtien = gvChiTiet.Rows[i].Cells["ThanhTien"].Value.ToString().Trim();
                        ChiTietPhieuBaoGiaBO.ThemChiTietPhieuBaoGia(machitietbaogia, mabaogia, madichvu, mahanghoa, dongia, soluong, thanhtien);
                    }


                }


                gvChiTiet.Rows.Clear();
                txtTongTien.Text = "0";
                MessageBox.Show("Bạn đã thêm vào thành công");
            }
        }

        private void btnThemKhachHang_Click(object sender, EventArgs e)
        {
            DanhSachKhachHangForm form = new DanhSachKhachHangForm();
            form.Show();
        }

        private void btnThemBienSo_Click(object sender, EventArgs e)
        {
            DanhSachXeForm form = new DanhSachXeForm();
            form.Show();
        }


    }
}
